package resoft.jeasyform.event;

import charva.awt.event.KeyEvent;
import charva.awt.event.KeyAdapter;
import resoft.jeasyform.components.InputField;

/**
 * <p>validation currency</p>
 * User: albert
 * Date: Aug 29, 2007
 * Time: 12:22:00 AM
 */
public class CurrencyValidationEventHandler extends KeyAdapter {
    public CurrencyValidationEventHandler(InputField field) {
        this.field = field;
    }


    public void keyPressed(KeyEvent keyEvent) {
        int keyCode = keyEvent.getKeyCode();
        if(isDigital(keyCode) || keyCode==46) {
            String value = field.getValue();
            if(value.indexOf(".")>0 && keyCode==46) {
                //prevent two .
                keyEvent.consume();
            }
        } else {
            keyEvent.consume();
        }
    }

    private boolean isDigital(int keyCode) {
        return keyCode>=48 && keyCode<=57;
    }


    private InputField field;
}
